using System;
using System.Drawing;
using FireStarterModeller.BO;
using FireStarterModeller.UI.Relationship;
using Habanero.Base;
using Habanero.BO;
using Habanero.Faces.Base;
using Habanero.Faces.Win;

namespace FireStarterModeller.UI
{
    /// <summary>
    /// Displays and edits the relationship with all its attributes, as well as it Relationship Properties.
    /// </summary>
    public class DMRelationshipEditorControl : UserControlWin, IBOEditorControl
    {
        private readonly IControlFactory _controlFactory;
        public IControlFactory ControlFactory { get { return _controlFactory; } }
//        private IPanelInfo _detailPanelInfo;
        private DMRelationshipPanelInfoControl _relationshipPanelInfo;
        private DMRelatedPropertiesControl _relationshipPropertiesControl;

        public DMRelationshipEditorControl(IControlFactory controlFactory)
        {
            if (controlFactory == null) throw new ArgumentNullException("controlFactory");
            _controlFactory = controlFactory;
            InitialiseControl();
            this.MinimumSize = new Size(400, 440);
        }

//
//        public ITextBox RelationshipNameTextBox { get; private set; }

        //public IPanelInfo DetailPanelInfo
        //{
        //    get { return RelationshipPanelInfo.DetailPanelInfo; }
        //}

        public IBusinessObjectControl RelationshipPropertiesControl
        {
            get { return _relationshipPropertiesControl; }
        }

        #region IBOEditorControl Members

        public IBusinessObject BusinessObject
        {
            get { return _relationshipPanelInfo.BusinessObject; }
            set
            {
                if (_relationshipPanelInfo.BusinessObject == value) return;
                _relationshipPanelInfo.BusinessObject = value;
                DMRelationship relationship = (DMRelationship) value;
                if (relationship != null && relationship.DMSortOrder == null)
                {
                    relationship.DMSortOrder = new DMSortOrder();
                }
                _relationshipPropertiesControl.BusinessObject = value;
            }
        }

        #endregion

        private void InitialiseControl()
        {
            _relationshipPanelInfo = new DMRelationshipPanelInfoControl(_controlFactory);
            _relationshipPropertiesControl = new DMRelatedPropertiesControl(_controlFactory);

            BorderLayoutManager layoutManager = _controlFactory.CreateBorderLayoutManager(this);
            layoutManager.AddControl(RelationshipPanelInfo, BorderLayoutManager.Position.North);
            layoutManager.AddControl(_relationshipPropertiesControl, BorderLayoutManager.Position.Centre);
            MinimumSize = new Size(RelationshipPanelInfo.MinimumSize.Width, 
                RelationshipPanelInfo.MinimumSize.Height 
                + RelationshipPanelInfo.MoreDetailPanel.MinimumSize.Height 
                + _relationshipPropertiesControl.MinimumSize.Height);
            Width = 329;
        }

        public void DisplayErrors()
        {
            _relationshipPanelInfo.DisplayErrors();
        }

        public void ClearErrors()
        {
            _relationshipPanelInfo.ClearErrors();
        }

        #region Implementation of IBOEditorControl

        /// <summary>
        ///             Applies any changes that have occured in any of the Controls on this control's to their related
        ///             Properties on the Business Object.
        /// </summary>
        public void ApplyChangesToBusinessObject()
        {
            this.RelationshipPanelInfo.ApplyChangesToBusinessObject();
        }

        /// <summary>
        ///             Does the business object controlled by this control or any of its Aggregate or Composite children have and Errors.
        /// </summary>
        public bool HasErrors
        {
            get { throw new NotImplementedException(); }
        }

        /// <summary>
        ///             Does the Business Object controlled by this control or any of its Aggregate or Composite children have and warnings.
        /// </summary>
        public bool HasWarning
        {
            get { throw new NotImplementedException(); }
        }

        /// <summary>
        ///              Returns a list of all warnings for the business object controlled by this control or any of its children.
        /// </summary>
        public ErrorList Errors
        {
            get { throw new NotImplementedException(); }
        }

        /// <summary>
        ///             Does the business object being managed by this control have any edits that have not been persisted.
        /// </summary>
        /// <returns>
        /// </returns>
        public bool IsDirty
        {
            get { throw new NotImplementedException(); }
        }

        /// <summary>
        ///             Returns a list of all warnings for the business object controlled by this control or any of its children.
        /// </summary>
        /// <returns>
        /// </returns>
        public ErrorList Warnings
        {
            get { throw new NotImplementedException(); }
        }

        public DMRelationshipPanelInfoControl RelationshipPanelInfo
        {
            get { return _relationshipPanelInfo; }
        }

        #endregion
    }
}